import { v4 as uuidv4 } from 'uuid';

export function toCamKey(obj, otherKey){
    if(obj instanceof Array){
        obj.forEach(item => {
            toCamKey(item)
        })
    } else if(obj instanceof Object){
        Object.keys(obj).forEach(function(key){
            if(key !== otherKey){
                let newKey = Camel(key)
                if(newKey !== key){
                    obj[newKey] = obj[key]
                    delete(obj[key])
                }
                toCamKey(obj[newKey])
            }else {

            }
        })
    }
    return obj
}

function Camel(str){
    return str.toLowerCase().replace(/_(\w)/g,function(first,letter){
        return letter.toUpperCase()
    })
}



export function GUID(){
    let guid = uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
    guid = guid.toUpperCase();
    return guid
}


export function setNullToEmpty(obj) {
    for (let key in obj) {
        if (obj[key] === null) {
            obj[key] = "";
        }
    }
}

export function sleep(time) {
    const beginTime = new Date().getTime()
    const endTime = beginTime + time
    while(true) {
        if (new Date().getTime() > endTime) {
            return
        }
    }
}
